<template>
  <el-button :size="size" :type="type" :plain="plain" :round="round" :circle="circle" :loading="loading" :disabled="disabled" :class="{'btn-disabled':disabled}" :autofocus="autofocus" @click="handleClickEvent(btnEvent, record, otherObj)" v-if="isShow">
    <slot>
      <div style="display: flex;justify-content: center;">
        <svg-icon v-if="eventObj.iconName" :icon-class="eventObj.iconName" style="font-size:14px" />
        <span v-html="eventObj.sjmc"></span>
      </div>
    </slot>
  </el-button>
</template>

<script>
// import { clickEvent } from "../event/eventClick";
// import { getEventObj, getEventByUnid } from "../api/module/event/event.js";
// import { mapGetters } from "vuex";
export default {
  name: "HCButton",
  data()
  {
    return {
      qx: null,
      btnEvent: null,
      //事件对象
      eventObj: {

      }
    };
  },
  props: {
    //事件unid
    eventunid:
    {
      type: String
    },
    btnObj: {
      type: Object,
      default: () =>
      {
        return {};
      },
    },
    otherObj: {
      type: [Object, Function],
    },
    record: {
      type: [Array, Object, Function],
    },
    size: {
      type: String,
    },
    type: {
      type: String,
      default: "primary",
    },
    plain: {
      type: Boolean,
      default: false,
    },
    round: {
      type: Boolean,
      default: false,
    },
    circle: {
      type: Boolean,
      default: false,
    },
    loading: {
      type: Boolean,
      default: false,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    autofocus: {
      type: Boolean,
      default: false,
    },
    autoTitle: {
      type: [String, Object, Boolean],
      default: "",
    }
  },
  //计算属性
  computed: {
    ...mapGetters([
      'roleKey','userInfo'
    ]),
    isShow()
    {
      console.log("触发计算属性")
      let valueToReturn = false;
      //是否是指定人员
      if(this.eventObj.zdyhunids)
      {
        let zdyhunidArr = this.eventObj.zdyhunids.split(",");
        if(zdyhunidArr.indexOf(this.userInfo.unid) > -1)
        {
          valueToReturn = true;
          return valueToReturn;
        }
        else
        {
          return false;
        }
      }

      if (this.eventObj.role || this.eventObj.qx)
      {
        //是否有角色
        let hasRole = false;
        if (this.eventObj.role)
        {
          let roleArr = this.eventObj.role.split(",");
          for (let index = 0; index < roleArr.length; index++)
          {
            const role = roleArr[index];
            let findIndex = this.roleKey.findIndex(pre_role =>
            {
              return pre_role.roleName == role;
            })
            if (findIndex > -1)
            {
              hasRole = true;
              break;
            }
          }
        }
        else
        {
          hasRole = true;
        }
        //是否有权限
        let hasQx = false;
        if (this.eventObj.qx)
        {
          let qxArr = this.eventObj.qx.split(",");
          let userQX = [];//用户在此节点有的权限
          if (this.qx)
          {
            userQX = this.qx.split(",");
          }
          for (let index = 0; index < qxArr.length; index++)
          {
            const qx = qxArr[index];
            if (userQX.indexOf(qx) > -1)
            {
              hasQx = true;
              break;
            }
          }
        }
        else
        {
          hasQx = true;
        }
        if (this.eventObj.gx == 1)
        {
          valueToReturn = hasRole && hasQx;
        }
        else
        {
          valueToReturn = hasRole || hasQx;
        }
      }
      else
      {
        if (this.eventObj.sjmc)
        {
          valueToReturn = true;
        }
        else
        {
          valueToReturn = false;
        }
      }
      return valueToReturn;
    }
  },
  // 跨层级调用父组件方法
  inject: {
    // 解决非fs进入视图报错的问题
    fsback: { value: "fsback", default: null }
  },
  methods: {
    clickEvent,
    //获取自定义事件对象
    getEventObj(P_Qx)
    {
      getEventObj({
        unid: this.eventunid
      }).then(res =>
      {
        this.eventObj = res.result || {};
        this.qx = P_Qx;
        getEventByUnid({
          unid: this.eventunid
        }).then(resEvent =>
        {
          this.btnEvent = resEvent.result || {};
        })
      })
    },
    //按钮点击
    handleClickEvent(P_BtnEvent, P_Record, P_otherObj)
    {
      console.log(P_BtnEvent, P_Record, P_otherObj)
      if (this.btnObj.checkedNumberBeforeClick)
      {
        if (P_Record.constructor === Array)
        {
          if (P_Record.length < this.btnObj.checkedNumberBeforeClick)
          {

            this.$message.warning("请至少勾选" + this.btnObj.checkedNumberBeforeClick + "项");
            return
          }
        }
      }
      if (window.HC_config.VUE_APP_FSANDFORM && this.btnObj.btnName == 'back')
      {
        if (this.btnObj.btnEvent.eventType == 'LINK_EVENT' || this.btnObj.btnEvent.eventType == 'EXECUTE_EVENT')
        {
          if (this.fsback)
          {
            this.fsback()
          }
          else
          {
            clickEvent(P_BtnEvent, P_Record, P_otherObj);
          }
        }
      }
      else
      {
        clickEvent(P_BtnEvent, P_Record, P_otherObj);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-button--text {
  margin-left: 10px;
  padding: 10px 12px;
  &:first-child {
    margin-left: 0;
  }
}
/*.el-button {
  margin-left: 10px;
  padding: 10px 12px;
  &:first-child {
    margin-left: 0;
  }
}*/
.icon_text {
  margin-left: 10px;
}

.btn-disabled {
  background-color: #c0c4cc !important;
  cursor: default !important;
  border-color: #c0c4cc !important;
}
</style>

